package com.lk.api.own;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.text.Html.TagHandler;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ClickableSpan;
import android.text.style.ImageSpan;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ProgressBar;
import android.widget.QuickContactBadge;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.lk.api.R;

import org.xml.sax.XMLReader;

import java.util.ArrayList;


public class ActivityUITextView extends Activity {
	ToggleButton toggleButton1;
	CheckBox checkBox;
	Context context = ActivityUITextView.this;
	ProgressBar progressBar;
	SeekBar seekBar;
	RatingBar ratingBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activityuitextview);
		spinner1();
	}

	private void ratingBar() {
		ratingBar = (RatingBar) findViewById(R.id.ratingBar1);
		ratingBar.setNumStars(5);
		ratingBar.setStepSize(1);
		ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
			@Override
			public void onRatingChanged(RatingBar ratingBar, float rating,
					boolean fromUser) {
				// TODO Auto-generated method stub
				Toast.makeText(context, "rating"+rating, Toast.LENGTH_LONG).show();

			}
		});
	}

	private void quickContactBadge() {
		QuickContactBadge quickContactBadge = (QuickContactBadge) findViewById(R.id.quickContactBadge1);
		quickContactBadge.assignContactFromPhone("15292455586", true);
	}

	private void seekBar() {
		seekBar = (SeekBar) findViewById(R.id.seekBar1);
		seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				Log.d("onStopTrackingTouch",
						"onStopTrackingTouch" + seekBar.getProgress());

			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				Log.d("onStartTrackingTouch",
						"onStartTrackingTouch" + seekBar.getProgress());
			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
				Log.d("onProgressChanged", "progress:" + progress
						+ " fromUser:" + fromUser + " onProgressChanged:"
						+ seekBar.getProgress());
			}
		});
		bgButton();
	}

	private void progressBar() {
		progressBar = (ProgressBar) findViewById(R.id.progressBar1);
		bgButton();
	}

	private void spinner1() {
		Spinner spinner = (Spinner) findViewById(R.id.spinner1);
		ArrayList<User> arrayList = new ArrayList<User>();
		arrayList.add(new User("lisi", "sa"));
		arrayList.add(new User("lisi1", "sa1"));
		arrayList.add(new User("lisi2", "sa2"));
		UserAdapter userAdapter = new UserAdapter(this, arrayList);
		spinner.setAdapter(userAdapter);

		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Toast.makeText(ActivityUITextView.this, "" + position, Toast.LENGTH_LONG)
						.show();
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub

			}
		});

	}

	private void spinner() {
		Spinner spinner = (Spinner) findViewById(R.id.spinner1);
		// spinner.setBackgroundResource(R.drawable.icon48x48_1);
		String[] data = { "1", "2", "3", "4", "5", "6" };
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_dropdown_item_1line, data);
		adapter.setDropDownViewResource(R.layout.drop_down_item);
		spinner.setAdapter(adapter);

		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Toast.makeText(context, ((TextView) view).getText().toString(),
						Toast.LENGTH_LONG).show();
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
			}
		});
	}

	private void radioGroup() {
		RadioGroup group = (RadioGroup) findViewById(R.id.ActivityUIradioGroup1);
		group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				switch (checkedId) {
				case R.id.ActivityUIradio0:
					Toast.makeText(context, "1 " + checkedId, Toast.LENGTH_LONG).show();
					break;
				case R.id.ActivityUIradio1:
					Toast.makeText(context, "2 " + checkedId, Toast.LENGTH_LONG).show();
					break;
				case R.id.ActivityUIradio2:
					Toast.makeText(context, "3 " + checkedId, Toast.LENGTH_LONG).show();
					break;

				default:
					break;
				}

			}
		});
	}

	private void checkbox() {
		checkBox = (CheckBox) findViewById(R.id.ActivityUIcheckBox);
		checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked == true) {
					Toast.makeText(context, "on", Toast.LENGTH_LONG).show();

				} else {
					Toast.makeText(context, "off", Toast.LENGTH_LONG).show();
				}

			}
		});
	}

	private void s() {

		toggleButton1 = (ToggleButton) findViewById(R.id.ActivityUItoggleButton);
		// getResources().getDrawable(R.drawable.icon48x48_1);
		// toggleButton1.setBackgroundColor(Color.BLUE);
		// toggleButton1.setBackgroundResource(R.drawable.icon48x48_1);
		toggleButton1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked == true) {
					toggleButton1.setBackgroundColor(Color.RED);
					// toggleButton1.setBackground(getResources().getDrawable(R.drawable.icon48x48_1));
					Toast.makeText(ActivityUITextView.this, "On " + isChecked,
                            Toast.LENGTH_LONG).show();
				} else {
					toggleButton1.setBackgroundColor(Color.BLUE);
					// toggleButton1.setBackground(getResources().getDrawable(R.drawable.icon48x48_2));
					Toast.makeText(ActivityUITextView.this, "Off " + isChecked,
                            Toast.LENGTH_LONG).show();
				}
			}
		});
	}

	private void bgButton() {
		Button button = (Button) findViewById(R.id.ActivityUIbutton);
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.d("ActivityUITextView", "on Click");


				// seekBar.setProgress(seekBar.getProgress() + 5);
				// if (seekBar.getProgress() >= seekBar.getMax()) {
				// seekBar.setProgress(0);
				// }

				// progressBar.setProgress(progressBar.getProgress() + a);
				// progressBar.setSecondaryProgress((progressBar.getProgress() +
				// a) + 2);
				// if (progressBar.getProgress() >= progressBar.getMax()) {
				// progressBar.setProgress(0);
				// progressBar.setSecondaryProgress(0);
				// }
				// progressBar.setProgress(5);
				// progressBar.setSecondaryProgress(10);Ԥ

			}
		});
		button.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				// TODO Auto-generated method stub
				Log.d("ActivityUITextView", "On Long Click");
				return true;
			}
		});
	}

	/**
	 */
	private void MySpannableImage() {
		TextView textView = (TextView) findViewById(R.id.ActivityUITextView);
		SpannableStringBuilder stringBuilder = new SpannableStringBuilder(
				"#hello,world");
		ImageSpan imageSpan = new ImageSpan(ActivityUITextView.this,
				R.drawable.icon48x48_1);

		ClickableSpan clickableSpan = new ClickableSpan() {

			@Override
			public void onClick(View widget) {
				// TODO Auto-generated method stub
				Toast.makeText(ActivityUITextView.this, "click", Toast.LENGTH_LONG).show();

			}
		};
		stringBuilder.setSpan(imageSpan, 6, 7,
				Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		stringBuilder.setSpan(clickableSpan, 1, 6,
				Spannable.SPAN_INCLUSIVE_INCLUSIVE);
		textView.setText(stringBuilder);
	}

	/**
	 */
	private void MyHtmlImage() {
		TextView textView = (TextView) findViewById(R.id.ActivityUITextView);
		TagHandler tagHandler = new TagHandler() {

			@Override
			public void handleTag(boolean opening, String tag, Editable output,
					XMLReader xmlReader) {
				Toast.makeText(ActivityUITextView.this, tag, Toast.LENGTH_LONG).show();
				// TODO Auto-generated method stub

			}
		};

		ImageGetter imageGetter = new ImageGetter() {

			@Override
			public Drawable getDrawable(String source) {
				// TODO Auto-generated method stub
				if (source != null) {
					BitmapDrawable returnDrawable = (BitmapDrawable) getResources()
							.getDrawable(R.drawable.icon48x48_1);
					returnDrawable.setBounds(0, 0,
							returnDrawable.getIntrinsicWidth(),
							returnDrawable.getIntrinsicHeight());
					return returnDrawable;
				} else {
					return null;
				}

			}
		};
		Spanned spanned = Html
				.fromHtml(
						"<html><div>hello</div><img src='http://www.baidu.com/img/bd_logo1.png' /> <div>world</a></html>",
						imageGetter, tagHandler);
		textView.setText(spanned);
	}

}
